package cn.iocoder.yudao.module.shop.service.itemconsume;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.shop.controller.admin.itemconsume.ShopItemConsumeCreateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.itemconsume.ShopItemConsumeRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.itemconsume.ShopItemConsumeUpdateReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.store.vo.StoreRespVO;
import cn.iocoder.yudao.module.shop.convert.itemconsume.ShopItemConsumeConvert;
import cn.iocoder.yudao.module.shop.convert.store.StoreConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemconsume.ShopItemConsumeDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.store.StoreDO;
import cn.iocoder.yudao.module.shop.dal.mysql.itemconsume.ShopItemConsumeMapper;
import cn.iocoder.yudao.module.shop.service.store.StoreService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.CONSUME_ITEM_NOT_EXISTS;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.ITEM_NOT_EXISTS;

@Service
@Validated
public class ShopItemConsumeServiceImpl implements ShopItemConsumeService{

    @Resource
    private ShopItemConsumeMapper shopItemConsumeMapper;

    @Resource
    private StoreService storeService;

    @Override
    public List<ShopItemConsumeRespVO> getList(Long itemId) {
        List<ShopItemConsumeDO> shopItemConsumeDOS = shopItemConsumeMapper.selectList(ShopItemConsumeDO::getItemId, itemId);
        List<Long> storeIds = new ArrayList<>();
        shopItemConsumeDOS.forEach(shopItemConsumeDO -> storeIds.add(shopItemConsumeDO.getStoreId()));

        List<StoreDO> storeDOList = storeService.getStoreList(storeIds);
        List<StoreRespVO> storeRespVOList = StoreConvert.INSTANCE.convertList(storeDOList);

        return ShopItemConsumeConvert.INSTANCE.convert(shopItemConsumeDOS, storeRespVOList);
    }

    @Override
    public ShopItemConsumeRespVO getByConsumeId(Long consumeId) {
        return ShopItemConsumeConvert.INSTANCE.convert(shopItemConsumeMapper.selectById(consumeId));
    }

    @Override
    public List<ShopItemConsumeRespVO> getList(List<Long> itemIds) {
        List<ShopItemConsumeRespVO> shopItemConsumeRespVOList = new ArrayList<>();

        itemIds.forEach(id -> {
            List<ShopItemConsumeRespVO> list = getList(id);
            shopItemConsumeRespVOList.addAll(list);
        });

        return shopItemConsumeRespVOList;
    }

    @Override
    public void deleteItem(Long itemId) {
        // 校验是否存在
        validateItemExists(itemId);

        // 删除
        shopItemConsumeMapper.deleteById(itemId);
    }

    @Override
    public void updateItem(ShopItemConsumeUpdateReqVO updateReqVO) {
        // 校验是否存在
        validateItemExists(updateReqVO.getId());
        ShopItemConsumeDO updateObj = ShopItemConsumeConvert.INSTANCE.convert(updateReqVO);
        shopItemConsumeMapper.updateById(updateObj);
    }

    @Override
    public void createItem(ShopItemConsumeCreateReqVO createReqVO) {
        ShopItemConsumeDO shopItemConsumeDO = ShopItemConsumeConvert.INSTANCE.convert(createReqVO);
        shopItemConsumeMapper.insert(shopItemConsumeDO);
    }

    private void validateItemExists(Long id) {
        if (shopItemConsumeMapper.selectById(id) == null) {
            throw exception(CONSUME_ITEM_NOT_EXISTS);
        }
    }
}
